package com.yocard.yxyd.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yocard.yxyd.adapter.FollowTagsAadapter;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.others.GridDividerDecoration;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.ConvertUtils;
import com.yocard.yxyd.utils.util.KeyboardUtils;
import com.yocard.yxyd.utils.util.StringUtils;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.ui.activity.base.BaseActivity;
import com.yocard.yxyd.ui.contract.AddflowContract;
import com.yocard.yxyd.ui.presenter.AddflowPresenter;
import com.yocard.yxyd.utils.SoftKeyBoardListener;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created on 2019/4/9.
 */
public class AddFollwActivity extends BaseActivity<AddflowPresenter> implements AddflowContract.View {

    @BindView(R.id.rv_follow_tag)
    RecyclerView rvFollowTag;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.scroll_view)
    ScrollView mScrollView;
    @BindView(R.id.et_note)
    AppCompatEditText etNote;
    @BindView(R.id.tv_lenth)
    TextView tvLenth;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    protected List<String> fields = new ArrayList<>();
    protected String type;
    protected String csmId;
    FollowTagsAadapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.act_add_follow;
    }

    @Override
    protected void initEventAndData() {
        initTitle();
        initSoftKeyBoard();

        showLoadingDialog();
        mPresenter.fetchStatus();
        Bundle extras = getIntent().getExtras();
        if(extras != null){
            csmId = extras.getString("csmId");
        }

        etNote.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.toString().trim().length();
                tvLenth.setText(length+"/50");
            }
        });
    }

    @OnClick({R.id.btn_submit,R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_submit:
                //防止短时间重复点击
                if (isFastDoubleClick()) return;

                KeyboardUtils.hideSoftInput(AddFollwActivity.this);
                String note = etNote.getText().toString().trim();
                if (!StringUtils.isEmpty(type)) {
                    showLoadingDialog();
                    mPresenter.commitTrack(csmId,type,note);
                } else {
                    ToastUtils.showShort("请选择跟进结果");
                }
                break;
            case R.id.iv_back:
                ActivityUtils.finishActivity(this);
                break;
        }

    }

    private void initTitle() {
        ImmersionBar.with(this).statusBarColor(R.color.white)
                .statusBarDarkFont(true).navigationBarColor(R.color.white).init();
        tvRight.setText("提交");
        tvRight.setVisibility(View.GONE);
        btnSubmit.setEnabled(false);
        tvTitle.setText("添加跟进记录");

    }

    private void initSoftKeyBoard() {
        SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                mScrollView.postDelayed(() -> {
                    mScrollView.fullScroll(View.FOCUS_DOWN);
                }, 0);
            }

            @Override
            public void keyBoardHide(int height) {
            }
        });
    }

    private void initFollowTags(List<String> strs){
        adapter = new FollowTagsAadapter(strs);
        HorizontalDividerItemDecoration horizontalBuild = new HorizontalDividerItemDecoration.Builder(mContext)
                .color(AppContext.getColor_(R.color.white))
                .size(ConvertUtils.dp2px(15))
                .build();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        rvFollowTag.setLayoutManager(gridLayoutManager);
        rvFollowTag.addItemDecoration(new GridDividerDecoration(ConvertUtils.dp2px(18), ConvertUtils.dp2px(14), 0));
        rvFollowTag.setAdapter(adapter);
        adapter.setOnItemclick(new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {
                type = CommonUtils.getStatusType(s);
                if (!StringUtils.isEmpty(type)) {
                    btnSubmit.setEnabled(true);
                } else {
                    btnSubmit.setEnabled(false);
                }
                KeyboardUtils.hideSoftInput(AddFollwActivity.this);

            }
        });
//        mFlowLayout.setAdapter(new TagAdapter<String>(strs) {
//
//            @Override
//            public View getView(FlowLayout parent, int position, String s) {
//                TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_flow_tv, mFlowLayout, false);
//                tv.setText(s);
//                return tv;
//            }
//        });
//
//        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
//            @Override
//            public boolean onTagClick(View view, int position, FlowLayout parent) {
//                TagView tv = (TagView) view;
//                if (tv.isChecked()) {
//                    type = fields.get(position);
//                } else {
//                    type = "";
//                }
//                if (!StringUtils.isEmpty(type)) {
//                    btnSubmit.setEnabled(true);
//                } else {
//                    btnSubmit.setEnabled(false);
//                }
//                KeyboardUtils.hideSoftInput(AddFollwActivity.this);
//                return true;
//            }
//        });
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onCommitSuccess() {
        EventBus.getDefault().post(new CommonBean(Constants.CODE_REFRESH));
        ActivityUtils.finishActivity(this);
    }

    @Override
    public void onFetStatusSuccess(Map<String, String> map) {
        dismisDialog();
        List<String> list = new ArrayList<>();
        list.addAll(map.values());
        initFollowTags(list);

        fields.addAll(map.keySet());
    }
}
